
package com.cn.ycy.config.interceptor;

import com.cn.ycy.dto.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class SecureUtil {
    private static final Logger log = LoggerFactory.getLogger(SecureUtil.class);

    private static final ThreadLocal<User> embedUserThreadLocal = new ThreadLocal<>();
    private static final ThreadLocal<Map<String, String>> requestHeaderThreadLocal = new ThreadLocal<Map<String, String>>() {
        protected Map<String, String> initialValue() {
            return new HashMap<>(4);
        }
    };

    public static void setCurrentEmbedUser(User embedUser) {
        embedUserThreadLocal.set(embedUser);
    }

    public static void clearCurrentEmbedUser() {
        embedUserThreadLocal.remove();
    }

    public static User getUser() {
        User embedUser = embedUserThreadLocal.get();
        if (null != embedUser) {
            return embedUser;
        }else{
            return null;
        }
    }

    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        return (requestAttributes == null) ? null : ((ServletRequestAttributes)requestAttributes).getRequest();
    }


}


